#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time       : 2025/8/6 8:26
# @Author     : cfg
# @File       : snake.py
# @Software   : PyCharm
# @Description: The description of this file.
import pygame
import pygame.gfxdraw

from sprites.role.snake_body import SnakeBody
from sprites.role.snake_header import SnakeHeader
from statics.color import Color
from statics.direction import Direction


class Snake(pygame.sprite.Sprite):
    def __init__(self, x: int, y: int, header_color: tuple = Color.player1, body_color: tuple = Color.brown):
        pygame.sprite.Sprite.__init__(self)
        self.length = 10
        self.score = 0
        self.is_died = False
        self.is_destroyed = False
        self.direction = Direction.right
        self.header = SnakeHeader(header_color)
        self.header.rect.center = (x, y)
        self.header_color = header_color
        self.body_color = body_color

        self.bodies = []
        self.body_rects = []
        for i in range(self.length - 1):
            body = SnakeBody(body_color)
            body.rect.center = (self.header.rect.center[0] - 2, self.header.rect.center[1])
            self.bodies.append(body)
            self.body_rects.append(body.rect)

        self.passed_coordinates = []
        self.ticks = 0

    def move(self, direction: str):
        old_x = self.header.rect.center[0]
        old_y = self.header.rect.center[1]
        if direction == Direction.left:
            new_x = old_x - 2
            self.header.rect.center = [new_x, old_y]
        if direction == Direction.right:
            new_x = old_x + 2
            self.header.rect.center = [new_x, old_y]
        if direction == Direction.up:
            new_y = self.header.rect.center[1] - 2
            self.header.rect.center = [old_x, new_y]
        if direction == Direction.down:
            new_y = self.header.rect.center[1] + 2
            self.header.rect.center = [old_x, new_y]
        self.passed_coordinates.append(self.header.rect.center)
        if len(self.passed_coordinates) > self.length:
            self.passed_coordinates = self.passed_coordinates[len(self.passed_coordinates)-self.length:]
        for idx, coord in enumerate(self.passed_coordinates[:-1]):
            self.bodies[idx].rect.center = coord
            self.body_rects[idx] = self.bodies[idx].rect

    def eat(self):
        for i in range(2):
            body = SnakeBody(self.body_color)
            body.rect.center = (self.header.rect.center[0] - 2, self.header.rect.center[1])
            self.bodies.append(body)
            self.body_rects.append(body.rect)
        self.length += 2

    def died(self):
        print(self)
        self.is_died = True
        self.header.color = Color.red
        for body in self.bodies:
            body.color = Color.red

    def reset(self, coord: tuple = (60, 120), header_color: tuple = Color.player1):
        self.__init__(coord[0], coord[1])

    def update(self, *args, **kwargs):
        if not self.is_died:
            self.move(self.direction)
        else:
            if not self.is_destroyed:
                self.ticks += 1
            if self.ticks > 120:
                self.is_destroyed = True
                self.header.kill()
                for body in self.bodies:
                    body.kill()
                self.kill()
